// import {getToken} from "./storageSync.js"
// const token = getToken()
import store from "@/store";
const METHOD = {
	GET: 'get',
	POST: 'post'
}

const duration = 2000
let request = function(reqUrl, method, param) {
	const token = store.getters.token
	return new Promise((resolve, reject) => {
		uni.request({
			url: reqUrl,
			data: param,
			method: method,
			timeout: 30000,
			dataType: 'json',
			header: {
				"token": token
			},
			complete: (res) => {
				if (res.statusCode == 200) {
					if (res.data.code == 0) { //登录已过期
						if (token) {
							returnMine('登录已过期,请重新登录')
							store.commit("user/removeUserToken")
						} else {
							returnMine('请先登录')
						}
					} else {
						resolve(res.data)
					}
				} else {
					reject(res)
				}
			}
		})
		
	}).catch(res => {
		console.log(res)
		uni.showToast({
			title: '服务器错误',
			icon: 'error',
			duration: duration
		});
	})

}


function   returnMine(text) {
      uni.switchTab({
				url: '/pages/mine/index',
				success:function(){
					uni.showToast({
						title: text,
						icon: 'none',
						duration: duration,
					});
				}
			});


}

export {
	request,
	METHOD
}
